package com.study.dataStructure.util;

import com.study.dataStructure.exception.ArrayCopyLengthException;
import com.study.dataStructure.list.array.MyArrayList;

import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;

/**
 * 数组工具类
 */
public class ArraysUtil {

    /**
     * 生成指定长度length大小,元素范围在[0,scope)内的整数数组
     * @param length 
     * @param scope
     * @return
     */
    public static int[] createRandomArr(int length, int scope) {
        int[] arr = new int[length];
        for (int i = 0; i < length; i++) {
            arr[i] = (int) ( Math.random() * scope ) ;
        }
        return arr;
    }

    /**
     * 判断数组是否是(按大小)排好序的
     * @param nums
     * @return
     */
    public static boolean isSort(int[] nums){
        if(nums.length>=0 && nums.length<=2) return true;
        Boolean flag = null ;
        for (int i = 1; i < nums.length; i++) {
//            boolean thisFlag = nums[i] - nums[i-1] >= 0;
            // 一定要是有数值差别才看得出是正序还是逆序
            Boolean thisFlag = null;
            int diff = nums[i] - nums[i-1] ;
            if(diff > 0)
                thisFlag = true; //正序
            else if(diff < 0)
                thisFlag = false; //逆序
            else
                thisFlag = null; //未知

            if(flag == null && thisFlag!= null)
                flag =  thisFlag; //第一次记录下正序还是逆序
            else if(thisFlag != null && !flag.equals(thisFlag))
                return false;
        }
        return true;
    }

    /**
     * 交换数组中两个元素位置
     * @param nums
     * @param i 下标
     * @param j 下标
     */
    public static void swap(int[] nums, int i, int j) {
        int temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
    }

    /**
     * 判断数组是否正序排列的
     * @param nums
     * @return
     */
    public static boolean isAsc(int[] nums){
        if(nums==null || nums.length<=2) return true;
        for (int i = 1; i < nums.length; i++) {
            if(nums[i]<nums[i-1]) {
                return false;
            }
        }
        return true;
    }

    public static String toString(int[] nums){
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < nums.length; i++) {
            sb.append(nums[i]+",");
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }

    public static void print(int[] nums){
        System.out.println(toString(nums));
    }

    public static void copyArray(int[] sourceArr, int[] aimArr) {
        if(aimArr.length<sourceArr.length){
            throw new ArrayCopyLengthException();
        }
        for (int i = 0; i < sourceArr.length; i++) {
            aimArr[i] = sourceArr[i];
        }
    }

    public static boolean serializeRandomArr(int length, int scope, String storePath){
        FileWriter fw = null;
        try{
            fw = new FileWriter(storePath);
            int[] arr = ArraysUtil.createRandomArr(25, 100);
            Properties props = new Properties();
            props.put("arr",ArraysUtil.toString(arr));
            props.store(fw,"save random-arr");
            return true;
        }catch (Exception e){
            System.out.println("序列化随机数组失败!");
            return false;
        }finally {
            if(fw!=null){
                try{fw.close();}catch (Exception e){}
            }
        }
    }

    public static int[] antiSerializationArr(String key, String storePath){
        FileReader reader = null;
        try{
            reader = new FileReader(storePath);
            Properties props = new Properties();
            props.load(reader);
            String arrString = props.getProperty(key);
            return stringToArr(arrString);
        }catch (Exception e){
            System.out.println("反序列化路径: "+storePath+"中的数组失败!");
            return null;
        }finally {
            if(reader!=null){
                try{reader.close();}catch (Exception e){}
            }
        }

    }

    public static int[] stringToArr(String arrString){
//        MyArrayList<Integer> list = new MyArrayList<>();
        String sub = arrString.substring(1, arrString.length() - 1);
        String[] strs = sub.split(",");
        int[] arr = new int[strs.length];
        for (int i = 0; i < strs.length; i++) {
            arr[i] = Integer.parseInt(strs[i]);
        }
        return arr;
    }
}
